package com.xiguanlezz.cn.course1;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

/**
 * @Author Chen Jie
 * @Date 2024/10/19 9:04
 * @Description 索引写入器
 * @Version 1.0
 */
public class Indexer {
    private IndexWriter writer;

    public static void main(String[] args) throws IOException {
        if (args.length != 2) {
            throw new IllegalArgumentException("Usage: java " + Indexer.class.getName() + " <index dir> <data dir>");
        }

        String indexDir = args[0]; /* index */
        String dataDir = args[1]; /* data */

        long start = System.currentTimeMillis();
        // 根据索引目录创建索引写入器
        Indexer indexer = new Indexer(indexDir);
        int numIndexed;
        try {
            // 根据数据文件目录写入索引
            numIndexed = indexer.index(dataDir, new TextFilesFilter());
        } finally {
            indexer.close();
        }

        long end = System.currentTimeMillis();

        System.out.println("Indexing " + numIndexed + " files took " + (end - start) + " milliseconds");
    }

    public Indexer(String indexDir) throws IOException {
        Directory dir = FSDirectory.open(new File(indexDir).toPath());
        // 分词器
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig iwConfig = new IndexWriterConfig(analyzer);
        writer = new IndexWriter(dir, iwConfig);
    }

    public int index(String dataDir, FileFilter filter) throws IOException {
        File[] files = new File(dataDir).listFiles();

        for (File f : files) {
            if (!f.isDirectory() && !f.isHidden() && f.exists() && f.canRead() &&
                    (filter == null || filter.accept(f))) {
                indexFile(f);
            }
        }

        return writer.numDocs();
    }

    protected Document getDocument(File f) throws IOException {
        // 取文件名
        String file1Name = f.getName();
        // 文件的路径
        String path = f.getPath();
        // 文件的内容
        String fileContext = FileUtils.readFileToString(f, "utf-8");

        // 创建Field
        // 参数1：域的名称，参数2：域的内容，参数3：是否储存
        Field fieldName = new TextField("name", file1Name, Field.Store.YES);
        Field fieldPath = new StoredField("path", path);
        Field fieldContext = new TextField("context", fileContext, Field.Store.YES);

        // 创建文档对象
        Document document = new Document();

        // 向文档对象中添加域
        document.add(fieldName);
        document.add(fieldPath);
        document.add(fieldContext);

        return document;
    }

    private void indexFile(File f) throws IOException {
        System.out.println("Indexing " + f.getCanonicalPath());
        // 将txt文件转化为文档对象
        Document doc = getDocument(f);
        // 将文档写入生成索引，writer中包含分词器
        writer.addDocument(doc);
    }


    public void close() throws IOException {
        writer.close();
    }

    private static class TextFilesFilter implements FileFilter {

        @Override
        public boolean accept(File pathname) {
            return pathname.getName().toLowerCase().endsWith(".txt");
        }
    }
}
